<?php
class Representatives extends Controller {

    private $roles = array('all','mps','advisors', 'mayors');

    private $language;
    function Representatives() {
        parent::Controller();
        session_start();
        $this->load->helper('my_language');
        $this->load->helper('debug');
        $this->load->model('representatives_model');
        $this->load->model('municipalities_model');
        $this->lang->load("front");
        $this->language = get_language_id($this->lang->lang());
    }

    function index() {
        redirect(base_url().$this->lang->lang());
    }

    function listRepresentatives($type = 'all') {
        //BITNO: Znaci, ako se klikne na Gradonacalnici ili na Sovetnici, treba soodvetno da se izvadat site. Na frontendot vo tabelata treba da pisuva
        //Opstina, a ako se izbere Pratenici, na treba da pisuva Izborna edinica. Soodvetno se vlecat i pretstavuvaat podatocite
        //ako se izbere Site, treba da smislime shto da pisuva, a treba soodvetno na sekoj red so sekoj predstavnik treba da dade opstina ili izbor.edin.

        if (!in_array($type, $this->roles)) redirect(base_url().$this->lang->lang());
       
        switch ($type) {
            case 'all':
                $representatives = $this->representatives_model->getAll($this->language, false);
                break;
            case 'mps':
                $representatives = $this->representatives_model->getAllPratenici($this->language, false);
                break;
            case 'advisors':
                $representatives = $this->representatives_model->getAllSovetnici($this->language, false);
                break;
            case 'mayors':
                $representatives = $this->representatives_model->getAllGradonacalnici($this->language, false);
                break;
        }
        $data['representatives'] = $representatives;
        $content['head'] = $this->load->view('head',null,true);
        $content['header'] = $this->load->view('header',null,true);
        $content['middleFront'] = $this->load->view('list_representatives', $data, true);
        $content['footer'] = $this->load->view('footer',null,true);
        $this->load->view('index_front',$content);
    }

    function pickRepresentative(){
        if(isset($_SESSION['sendData'])){
            unset($_SESSION['sendData']);
        }
        $data['municipality']= $this->getCurrentMunicipalityModel();
        $this->load->model('representatives_model');
	//var_dump($data['municipality'][0]->izborna_edinica_id);
        $data['pratenici'] = $this->representatives_model->getAllPratenici(get_language_id($this->lang->lang()), $data['municipality'][0]->izborna_edinica_id);
        $data['sovetnici'] = $this->representatives_model->getAllSovetnici(get_language_id($this->lang->lang()), $data['municipality'][0]->municipality_id);
        $data['gradonacalnici'] = $this->representatives_model->getAllGradonacalnici(get_language_id($this->lang->lang()),$data['municipality'][0]->municipality_id);
        //var_dump($data);
        //die;

        $content['head'] = $this->load->view('head',null,true);
        $content['header'] = $this->load->view('header',null,true);
        $content['middleFront'] = $this->load->view('pick_representative',$data,true);
        $content['footer'] = $this->load->view('footer',null,true);
        $this->load->view('index_front',$content);
    }

    function answers($representative_id = 1) {
        $data['representative'] = $this->representatives_model->getRepresentativeById($representative_id); //print_r($data['representative']);die();
        $data['message_answers'] = $this->representatives_model->getRepAnswersForMessages($representative_id);
//        print_r($data['message_answers']); die();
        $data['other_representatives'] = $this->representatives_model->getOtherRepMessages($representative_id);
        $content['head'] = $this->load->view('head',null,true);
        $content['header'] = $this->load->view('header',null,true);
        $content['middleFront'] = $this->load->view('questions_answers',$data,true);
        $content['footer'] = $this->load->view('footer',null,true);
        $this->load->view('index_front',$content);
    }

    function getCurrentMunicipalityModel(){
        $this->load->model('municipalities_model');
        $munModel = $this->municipalities_model->getMunicipalityById($this->session->userdata('municipality_id'));
        return $munModel;
    }

    function test()
    {
        $this->representatives_model->getOtherRepMessages(9);
    }

    function listaj() {
        $content['head'] = $this->load->view('head',null,true);
        $content['header'] = $this->load->view('header',null,true);
        $content['middleFront'] = $this->load->view('list_representatives',null,true);
        $content['footer'] = $this->load->view('footer',null,true);
        $this->load->view('index_front',$content);
    }

//    function

}
